export const crudCode = `
import {PageEntity} from '../core/domain/page.entity';
import {StatusEnum} from '../core/enum/status.enum';
import {PaginationUtils} from './paginationUtils';
import {OtherUtil} from './other.util';
import {StorageUtil} from './storage.util';
import {FilterEnum, FilterTypeEnum} from '../core/enum/filter.enum';
import {PostDown} from './postdown';
import { cloneDeep } from 'lodash';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';

/**
 * 服务映射
 * @param http http服务
 * @param message 消息服务
 * @param postDown 导出服务
 * @param that 组件this
 * @param initPageInfo 初始化分页信息
 * @param transDataList 转换dataList数据
 * @param exportUrl 导出地址
 * @param isOtherPage 是否是其他分页
 * @param isThirdPage 是否是第三个分页
 * @param isFourthPage 是否是第四个分页
 * @param tableResetListKey 列表重置字段
 * @param defaultSor 默认排序方式
 */
export interface ServiceMap {
  http?: any;
  message?: NzMessageService;
  postDown?: PostDown;
  that?: any;
  modalService?: NzModalService;
  initPageInfo?: Function;
  transDataList?: Function;
  exportUrl?: string;
  isOtherPage?: boolean;
  isThirdPage?: boolean;
  isFourthPage?: boolean;
  tableResetListKey?: string[];
  defaultSort?: {
    [key: string]: 'descend' | 'ascend' | null
  };
}

/**
 * 增删改查的基类
 * @param serviceMap 服务映射
 * @param searchParams 查询参数
 */
class Crud {
  pageInfo: PageEntity = new PageEntity();
  searchParam = new this.searchParams();
  dataList = [];
  isAllCheck = false;
  isIndeterminate = false;
  mapOfCheckedId = {};
  canDelete = true;
  constructor(
    public serviceMap: ServiceMap,
    private searchParams: any,
  ) {}

  /**
   * 分页查询
   * @param page 分页参数
   */
  page(page) {
    if (this.serviceMap.defaultSort) {
      let sortKey = '';
      let sortValue = null;
      for (const o in this.serviceMap.defaultSort) {
        if (this.serviceMap.defaultSort[o]) {
          sortKey = o;
          sortValue = this.serviceMap.defaultSort[o];
        }
      }
      this.pageInfo.multiSortMeta = sortValue ? [
        {
          field: sortKey,
          order: sortValue === 'descend' ? -1 : 1
        }
      ] : [];
    }
    if (this.serviceMap.isOtherPage) {
      this.serviceMap.http.otherPageApi(page).subscribe(res => {
        if (res.status === StatusEnum.SUCCESS) {
          if (this.serviceMap.transDataList) {
            this.dataList = this.serviceMap.transDataList(res.datas.data, this.serviceMap.that);
          } else {
            this.dataList = res.datas.data;
          }
          this.pageInfo.totalRecords = res.datas.totalRecords;
        }
      });
    } else if (this.serviceMap.isThirdPage) {
      this.serviceMap.http.thirdPageApi(page).subscribe(res => {
        if (res.status === StatusEnum.SUCCESS) {
          if (this.serviceMap.transDataList) {
            this.dataList = this.serviceMap.transDataList(res.datas.data, this.serviceMap.that);
          } else {
            this.dataList = res.datas.data;
          }
          this.pageInfo.totalRecords = res.datas.totalRecords;
        }
      });
    } else if (this.serviceMap.isFourthPage) {
      this.serviceMap.http.fourthPageApi(page).subscribe(res => {
        if (res.status === StatusEnum.SUCCESS) {
          if (this.serviceMap.transDataList) {
            this.dataList = this.serviceMap.transDataList(res.datas.data, this.serviceMap.that);
          } else {
            this.dataList = res.datas.data;
          }
          this.pageInfo.totalRecords = res.datas.totalRecords;
        }
      });
    } else {
      this.serviceMap.http.page(page).subscribe(res => {
        if (res.status === StatusEnum.SUCCESS) {
          if (this.serviceMap.transDataList) {
            this.dataList = this.serviceMap.transDataList(res.datas.data, this.serviceMap.that);
          } else {
            this.dataList = res.datas.data;
          }
          this.pageInfo.totalRecords = res.datas.totalRecords;
        }
      });
    }
  }

  /**
   * 点击查询
   */
  clickSearch() {
    // tslint:disable-next-line:forin
    for (const o in this.searchParam) {
      if (this.searchParam[o]) {
        this.pageInfo.filters[o] = PaginationUtils.filters(this.searchParam[o], FilterEnum.CONTAINS);
      } else {
        delete this.pageInfo.filters[o];
      }
      if (typeof this.searchParam[o] === 'number') {
        this.pageInfo.filters[o] = PaginationUtils.filters(this.searchParam[o], FilterEnum.EQUALS);
      }
    }
    this.pageInfo.first = 1;
    this.page(this.pageInfo);
  }

  /**
   * 点击重置
   */
  clickReset() {
    this.pageInfo = new PageEntity();
    this.searchParam = {};
    this.cancelFn();
    if (this.serviceMap.initPageInfo) {
      this.serviceMap.initPageInfo(this.pageInfo, this.serviceMap.that);
    }
    // 重置排序样式
    // tslint:disable-next-line:forin
    for (const o in this.serviceMap.defaultSort) {
      this.serviceMap.defaultSort[o] = null;
    }
    // 重置表头选中样式
    const listNameMap = {};
    (this.serviceMap.tableResetListKey || []).map(listName => {
      listNameMap[listName] = cloneDeep(this.serviceMap.that[listName]);
      listNameMap[listName].map(item => {
        item.byDefault = false;
      });
      this.serviceMap.that[listName] = listNameMap[listName];
    });
    this.clickSearch();
  }

  /**
   * 重置表格
   * @param field 字段
   */
  resetTable(field) {
    this.searchParam[field] = '';
    this.clickSearch();
  }

  /**
   * 多选过滤
   * @param event 值
   * @param field 字段
   */
  filterCheckField(event, field) {
    if (event && event.length > 0) {
      this.pageInfo.first = 1;
      this.pageInfo.filters[field] = {
        matchMode: FilterEnum.IN,
        value: typeof event === 'object' ? event.join(',') : event,
        whereType: FilterTypeEnum.AND
      };
    } else {
      delete this.pageInfo.filters[field];
    }
    this.clickSearch();
  }

  filterSingleCheckField(event, field) {
    if (event !== null) {
      this.pageInfo.first = 1;
      this.pageInfo.filters[field] = {
        matchMode: FilterEnum.EQUALS,
        value: event,
      };
    } else {
      delete this.pageInfo.filters[field];
    }
    this.clickSearch();
  }

  /**
   * 全选
   * @param value 是否全选
   */
  checkAll(value: boolean): void {
    this.dataList.forEach(item => (this.mapOfCheckedId[item.id] = value));
    this.refreshStatus();
  }

  refreshStatus() {
    this.isAllCheck = this.dataList.every(item => this.mapOfCheckedId[item.id]);
    this.isIndeterminate =
      this.dataList.some(item => this.mapOfCheckedId[item.id]) &&
      !this.isAllCheck;
    this.checkDelete();
  }

  /**
   * 导出
   * @param mode 1 导出选中  3 导出全部
   */
  exportShow(mode) {
    if (!this.serviceMap.exportUrl) {
      this.serviceMap.message.error('导出地址不能为空！');
      return;
    }
    const map = {
      mode,
      pager: {},
      sourceIds: []
    };
    if (mode === 1) {
      map.sourceIds = Object.keys(this.mapOfCheckedId).filter(item => this.mapOfCheckedId[item] === true);
    } else if (mode === 3) {
      map.pager = this.pageInfo;
    }
    if (mode === 1 && map.sourceIds.length <= 0) {
      this.serviceMap.message.warning('请选择需要导出的数据!');
      return;
    }
    if (!this.serviceMap.postDown) {
      this.serviceMap.message.error('postDown服务不能为空！');
      return;
    }
    this.serviceMap.postDown.postDownload(this.serviceMap.exportUrl, map);
  }

  /**
   * 获取选中的id数组
   */
  getCheckRowKeys() {
    return Object.keys(this.mapOfCheckedId).filter(item => this.mapOfCheckedId[item] === true);
  }

  /**
   * 获取选中的行
   */
  getCheckedRows() {
    return this.dataList.filter(item => this.mapOfCheckedId[item.id] === true);
  }

  /**
   * 切换页码
   * @param index 第几页
   */
  searchPageIndex(index: number) {
    this.pageInfo.first = index;
    this.page(this.pageInfo);
  }

  /**
   * 切换每页显示数量
   * @param size 每页显示数量
   */
  searchPageSize(size: number) {
    this.pageInfo.first = 1;
    this.pageInfo.rows = size;
    this.page(this.pageInfo);
  }

  /**
   * 能否删除
   */
  checkDelete(): void {
    this.canDelete = OtherUtil.deleteIds(this.mapOfCheckedId).length <= 0;
  }

  /**
   * 取消删除
   */
  cancelFn(): void {
    this.canDelete = true;
    this.isAllCheck = false;
    this.mapOfCheckedId = {};
    this.isIndeterminate = false;
  }

  /**
   * 删除一个
   */
  deleteOne(id: string) {
    this.serviceMap.http.delete(id).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.isAllCheck = false;
        this.isIndeterminate = false;
        this.mapOfCheckedId = {};
        this.checkDelete();
        this.serviceMap.message.success(res.message);
        this.page(StorageUtil.getPageFirst(this.pageInfo));
      }
    });
  }

  /**
   * 删除选中
   */
  deleteAll() {
    const arr = [];
    for (const val in this.mapOfCheckedId) {
      if (this.mapOfCheckedId[val]) {
        arr[arr.length] = val;
      }
    }
    if (arr.length < 1) {
      this.serviceMap.message.warning('请选择需要删除的记录!');
      return;
    }
    if (!this.serviceMap.http.batchDeleteApi) {
      this.serviceMap.message.error('请在service中定义batchDeleteApi方法');
      return;
    }
    const modal = this.serviceMap.modalService.create({
      nzTitle: '提示',
      nzContent: '<div><div class="colred m-b-sm font-size-16">您确定要删除吗?</div><div class="colblue">已勾选 ' + arr.length + '</div></div>',
      nzClosable: false,
      nzMaskClosable: false,
      nzFooter: [
        {
          label: '确定',
          type: 'default',
          onClick: () => {
            this.serviceMap.http.batchDeleteApi(arr).subscribe(res => {
              if (res.status === StatusEnum.SUCCESS) {
                this.isAllCheck = false;
                this.isIndeterminate = false;
                this.mapOfCheckedId = {};
                this.checkDelete();
                this.serviceMap.message.success(res.message);
                this.clickSearch();
                modal.destroy();
              }
            });
          }
        },
        {
          label: '取消',
          type: 'danger',
          onClick: () => modal.destroy()
        },
      ]
    });
  }

  /**
   * 批量启用
   */
  batchEnable() {
    const arr = [];
    for (const val in this.mapOfCheckedId) {
      if (this.mapOfCheckedId[val]) {
        arr[arr.length] = val;
      }
    }
    if (arr.length < 1) {
      this.serviceMap.message.warning('请选择需要启用的记录!');
      return;
    }
    if (!this.serviceMap.http.batchEnableApi) {
      this.serviceMap.message.error('请在service中定义batchEnableApi方法');
      return;
    }
    const modal = this.serviceMap.modalService.create({
      nzTitle: '提示',
      nzContent: '<div><div class="colred m-b-sm font-size-16">您确定要启用吗?</div><div class="colblue">已勾选 ' + arr.length + '</div></div>',
      nzClosable: false,
      nzMaskClosable: false,
      nzFooter: [
        {
          label: '确定',
          type: 'default',
          onClick: () => {
            this.serviceMap.http.batchEnableApi(arr).subscribe(res => {
              if (res.status === StatusEnum.SUCCESS) {
                this.isAllCheck = false;
                this.isIndeterminate = false;
                this.mapOfCheckedId = {};
                this.checkDelete();
                this.serviceMap.message.success(res.message);
                this.clickSearch();
                modal.destroy();
              }
            });
          }
        },
        {
          label: '取消',
          type: 'danger',
          onClick: () => modal.destroy()
        },
      ]
    });
  }

  /**
   * 批量禁用
   */
  batchDisable() {
    const arr = [];
    for (const val in this.mapOfCheckedId) {
      if (this.mapOfCheckedId[val]) {
        arr[arr.length] = val;
      }
    }
    if (arr.length < 1) {
      this.serviceMap.message.warning('请选择需要禁用的记录!');
      return;
    }
    if (!this.serviceMap.http.batchDisableApi) {
      this.serviceMap.message.error('请在service中定义batchDisableApi方法');
      return;
    }
    const modal = this.serviceMap.modalService.create({
      nzTitle: '提示',
      nzContent: '<div><div class="colred m-b-sm font-size-16">您确定要禁用吗?</div><div class="colblue">已勾选 ' + arr.length + '</div></div>',
      nzClosable: false,
      nzMaskClosable: false,
      nzFooter: [
        {
          label: '确定',
          type: 'default',
          onClick: () => {
            this.serviceMap.http.batchDisableApi(arr).subscribe(res => {
              if (res.status === StatusEnum.SUCCESS) {
                this.isAllCheck = false;
                this.isIndeterminate = false;
                this.mapOfCheckedId = {};
                this.checkDelete();
                this.serviceMap.message.success(res.message);
                this.clickSearch();
                modal.destroy();
              }
            });
          }
        },
        {
          label: '取消',
          type: 'danger',
          onClick: () => modal.destroy()
        },
      ]
    });
  }

  /**
   * 排序
   * @param sort 排序对象
   */
  sort(sort): void {
   if (this.serviceMap.defaultSort) {
     // tslint:disable-next-line:forin
     for (const o in this.serviceMap.defaultSort) {
       this.serviceMap.defaultSort[o] = null;
       if (sort.key === o) {
         this.serviceMap.defaultSort[o] = sort.value;
       }
     }
   } else {
     this.pageInfo = PaginationUtils.sort(this.pageInfo, sort);
   }
    this.page(this.pageInfo);
  }
}
export default Crud;

`;
